from locust import HttpUser, TaskSet, task, between
import random
import string

ids_new = [1, 2, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 167, 168, 169, 200, 201, 202, 203, 204, 205, 206, 207, 208]


class UserBehavior(TaskSet):

    def on_start(self):
        """ Called when a simulated user starts executing the task set """
        self.login()
    def generate_username(self):
        # 生成一个随机的用户名
        return 'user_' + ''.join(random.choices(string.ascii_lowercase + string.digits, k=8))

    def login(self):
        # 注册一个新用户（只执行一次）
        self.username = self.generate_username()
        self.password = "password"
        with self.client.post("/user/login/register/withoutVerify", json={
            "userName": self.username,
            "password": self.password
        }, catch_response=True) as response:
            if response.status_code == 200:
                print("User registered")

        # 登录用户
        with self.client.post("/user/login/loginWithoutVerify", json={
            "userName": self.username,
            "password": self.password
        }, catch_response=True) as response:
            if response.status_code == 200:
                print("User logged in")

    @task(2)
    def get_user_info(self):
        # 获取用户信息
        self.client.get("/user/userInfo")

    @task(3)
    def get_blog_paginated(self):
        # 分页获取博客
        self.client.get("/blog/full", params={"page": 0, "size": 10})

    @task(3)
    def get_blog_by_category_or_tag(self):
        # 根据分类或标签查询博客 有点问题
        select = random.randint(0, 1)
        if select==0:
            self.client.get("/blog/full/categoryOrTag", params={
                "category": "后台",
                "shortContent": True,
                "page": 0,
                "size": 10
            })
        else:
            self.client.get("/blog/full/categoryOrTag", params={
                "tag": "k8s",
                "shortContent": True,
                "page": 0,
                "size": 10
            })

    @task(3)
    def search_blog_without_content(self):
        # 查询博客不包含内容
        self.client.get("/blog/fullWithoutContent", params={"page": 0, "size": 10})

    @task(1)
    def get_single_blog(self):
        random_id_new = random.choice(ids_new)
        # 查询某一个博客
        self.client.get("/blog/full/one", params={"id": random_id_new})

    @task(2)
    def search_blog_like(self):
        # 模糊查询博客
        self.client.get("/blog/full/like", params={"title": "java", "shortContent": True})

    @task(2)
    def search_blog_with_search_engine(self):
        # 模糊查询博客（使用搜索引擎）
        self.client.get("/blog/full/like/v2", params={"title": "java", "shortContent": True})

    @task(1)
    def get_hot_blogs(self):
        # 获取热点文章前10
        self.client.get("/blog/hot")

    @task(1)
    def get_top_blogs(self):
        # 获取置顶文章
        self.client.get("/blog/top")

    @task(1)
    def get_blog_views(self):
        random_id_new = random.choice(ids_new)
        # 获取某篇文章的观看人数
        self.client.put("/blog/view", params={"id": random_id_new})

    @task(1)
    def get_category_info(self):
        # 获取分类数量和分类信息
        self.client.get("/category/all/size")

class WebsiteUser(HttpUser):
    tasks = [UserBehavior]
    wait_time = between(1, 5)

